package com.csht.controller;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.csht.model.CshtUser;
import com.csht.page.PagerModel;
import com.csht.page.SystemContext;
import com.csht.service.CshtGoodsService;
import com.csht.vo.my.GoodsAndUserVo;

/**
 * 商品列表
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/goods")
public class GoodsListController {
	
	@Autowired
	private CshtGoodsService cshtGoodsService;

	/**
	 * 一级菜单商品
	 * @param model
	 * @param oneMenuId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/oneMenuGoods/{oneMenuId}")
	public String oneMenuGoods(ModelMap model, @PathVariable Integer oneMenuId, HttpServletRequest request) 
			throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("oneMenuId", oneMenuId);
		res.put("goodsListPageSize", SystemContext.getGoodsListPageSize());
		res.put("offset", SystemContext.getOffset());
		String userAddress = this.getUserAddrees(request); //获取用户学校地址
		res.put("goodsTradeSchool", userAddress);
		PagerModel<GoodsAndUserVo> pagerModel = cshtGoodsService.selectGoodsByOneMenuId(res);
		model.addAttribute("pagerModel", pagerModel);
		model.addAttribute("MenuOrBrandUrl", "oneMenuGoods/" + oneMenuId);
		return "/goods/goodsList";
	}
	
	/**
	 * 二级菜单商品
	 * @param model
	 * @param twoMenuId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/twoMenuGoods/{twoMenuId}")
	public String twoMenuGoods(ModelMap model, @PathVariable Integer twoMenuId, HttpServletRequest request) 
			throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("twoMenuId", twoMenuId);
		res.put("goodsListPageSize", SystemContext.getGoodsListPageSize());
		res.put("offset", SystemContext.getOffset());
		String userAddress = this.getUserAddrees(request); //获取用户学校地址
		res.put("goodsTradeSchool", userAddress);
		PagerModel<GoodsAndUserVo> pagerModel = cshtGoodsService.selectGoodsByTwoMenuId(res);
		model.addAttribute("pagerModel", pagerModel);
		model.addAttribute("MenuOrBrandUrl", "twoMenuGoods/" + twoMenuId);
		return "/goods/goodsList";
	}
	
	/**
	 * 三级菜单商品
	 * @param model
	 * @param threeMenuId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/threeMenuGoods/{threeMenuId}")
	public String threeMenuGoods(ModelMap model, @PathVariable Integer threeMenuId, HttpServletRequest request) 
			throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("threeMenuId", threeMenuId);
		res.put("goodsListPageSize", SystemContext.getGoodsListPageSize());
		res.put("offset", SystemContext.getOffset());
		String userAddress = this.getUserAddrees(request); //获取用户学校地址
		res.put("goodsTradeSchool", userAddress);
		PagerModel<GoodsAndUserVo> pagerModel = cshtGoodsService.selectGoodsByThreeMenuId(res);
		model.addAttribute("pagerModel", pagerModel);
		model.addAttribute("MenuOrBrandUrl", "threeMenuGoods/" + threeMenuId);
		return "/goods/goodsList";
	}
	
	/**
	 * 按品牌搜索商品
	 * @param model
	 * @param brandId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/brandGoods/{brandId}")
	public String brandGoods(ModelMap model, @PathVariable Integer brandId, HttpServletRequest request) 
			throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("brandId", brandId);
		res.put("goodsListPageSize", SystemContext.getGoodsListPageSize());
		res.put("offset", SystemContext.getOffset());
		String userAddress = this.getUserAddrees(request); //获取用户学校地址
		res.put("goodsTradeSchool", userAddress);
		PagerModel<GoodsAndUserVo> pagerModel = cshtGoodsService.selectGoodsByBrandId(res);
		model.addAttribute("pagerModel", pagerModel);
		model.addAttribute("MenuOrBrandUrl", "brandGoods/" + brandId);
		return "/goods/goodsList";
	}
	
	/**
	 * 根据关键词搜索商品(一、二、三级菜单，商品标题、品牌)
	 * @param model
	 * @param goodssousuoval
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sousuo/{goodsSouSuoVal}")
	public String sousuo(ModelMap model, @PathVariable String goodsSouSuoVal, HttpServletRequest request) 
			throws Exception{
		String SouSuoVal = "%" + goodsSouSuoVal + "%";
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("goodsSouSuoVal", SouSuoVal);
		res.put("goodsListPageSize", SystemContext.getGoodsListPageSize());
		res.put("offset", SystemContext.getOffset());
		String userAddress = this.getUserAddrees(request); //获取用户学校地址
		res.put("goodsTradeSchool", userAddress);
		PagerModel<GoodsAndUserVo> pagerModel = cshtGoodsService.selectGoodsBysousuo(res);
		model.addAttribute("pagerModel", pagerModel);
		model.addAttribute("MenuOrBrandUrl", "sousuo/" + goodsSouSuoVal);
		return "/goods/goodsList";
	}
	
	/**
	 * 根据用户id查找该用户的商品
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getGoodsListByUserId/{userId}")
	public String getGoodsListByUserId(ModelMap model, @PathVariable Integer userId) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("userId", userId);
		res.put("goodsListPageSize", SystemContext.getGoodsListPageSize()); 
		res.put("offset", SystemContext.getOffset());
		PagerModel<GoodsAndUserVo> pagerModel = cshtGoodsService.selectGoodsListByUserId(res);
		model.addAttribute("pagerModel", pagerModel);
		model.addAttribute("MenuOrBrandUrl", "getGoodsListByUserId/" + userId);
		return "/goods/goodsList";
	}
	
	/**
	 * 判断是否有登录，有的话就根据地址取数据
	 * @return
	 * @throws Exception
	 */
	public String getUserAddrees(HttpServletRequest request) throws Exception{
		String address = "";
		String provinceName = "";
		String cityName = "";
		String schoolName = "";
		Cookie[] c = request.getCookies();
		for(int i = 0; i < c.length; i++) {
			if("provinceName".equals(c[i].getName())) {
				provinceName = URLDecoder.decode(c[i].getValue(), "utf-8");
			}
			if("cityName".equals(c[i].getName())) {
				cityName = URLDecoder.decode(c[i].getValue(), "utf-8");
			}
			if("schoolName".equals(c[i].getName())) {
				schoolName = URLDecoder.decode(c[i].getValue(), "utf-8");
			}
		}
		if(!provinceName.equals("") && !provinceName.equals("全部省份")) {
			address += provinceName;
		}
		if(!cityName.equals("") && !cityName.equals("全部城市")) {
			address += "-" + cityName;
		}
		if(!schoolName.equals("") && !schoolName.equals("全部学校")) {
			address += "-" + schoolName;
		}
		address = "%" + address + "%";
		System.out.println("学校定位：" + address);
		
		return address;
	}
	
	
}
